<?php
global $_W, $_GPC;
require_once IA_ROOT . '/addons/feng_lottery/site.php';
$site=new Feng_lotteryModuleSite();
$site -> backlists();
checklogin();
//$this -> checkmode();
$gettime = $site -> module['config']['gettime'];//自动签收时间
$weid = $_W['uniacid'];
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
if ($operation == 'display') {
        ca('order.view');
	//更新团状态
	$site->updategourp();
	//更新团状态
	$pindex = max(1, intval($_GPC['page']));
	$psize = 20;
	$condition = "  uniacid = :weid";
	$paras = array(':weid' => $_W['uniacid']);
	$status = $_GPC['status'];
	$transid = $_GPC['transid'];
	$pay_type = $_GPC['pay_type'];
	$keyword = $_GPC['keyword'];
	$member = $_GPC['member'];
	$time = $_GPC['time'];
        $starttime = empty($_GPC['time']['start']) ? strtotime('-1 month') : strtotime($_GPC['time']['start']);
        $endtime = empty($_GPC['time']['end']) ? time() : strtotime($_GPC['time']['end']);
	if ($_GPC['searchtime']==1) {
		$condition .= " AND  createtime >= :starttime AND  createtime <= :endtime ";
		$paras[':starttime'] = $starttime;
		$paras[':endtime'] = $endtime;
	}
	if (!empty($_GPC['transid'])) {
		$condition .= " AND  transid =:transid ";
                $paras[':transid'] = trim($_GPC['transid']);
	}
	if (!empty($_GPC['paytype']) || $_GPC['paytype']==='0') {
		$condition .= " AND  pay_type =:paytype ";
                $paras[':paytype'] =$_GPC['paytype'];
	}
	if (!empty($_GPC['ordersn'])) {
		$condition .= " AND  orderno LIKE :ordersn ";
                $paras[':ordersn'] ="%{$_GPC['ordersn']}%";
	}
	if (!empty($_GPC['member'])) {
		$condition .= " AND (addname LIKE '%{$_GPC['member']}%' or mobile LIKE '%{$_GPC['member']}%')";
	}
        if(!empty($_GPC['expresssn'])){
            $condition .= ' AND expresssn=:expresssn';
            $paras[':expresssn'] = trim($_GPC['expresssn']);
        }
	if ($status != '') {
                if($status==8){
		$condition .= " AND  status = '2' AND tuan_id<>0";
                }elseif($status==2){
                $condition .= " AND  status = '" . intval($status) . "' AND islottery=0 AND tuan_id=0";     
                }elseif($status==9){
                $condition .= " AND  status = '2' AND tuan_id<>0 AND islottery=1";   
                }else{
                $condition .= " AND  status = '" . intval($status) . "'";    
                }
		if($status==4){
			$allnogettime = pdo_fetchall("select * from".tablename('lottery_order')."where gettime='' and uniacid='{$_W['uniacid']}' and status=3");
			if(empty($gettime)){
				$gettime = 5;
			}
			$now = time();
			foreach($allnogettime as $key =>$value){
				$shouldgettime = $value['sendtime']+$gettime*24*3600;
				if($shouldgettime<$now){
					pdo_update('lottery_order',array('gettime'=>$shouldgettime,'status'=>4),array('id'=>$value['id']));
				}
			}
			
		}
	}
	$sql = "select  * from " . tablename('lottery_order') . " where $condition and mobile<>'虚拟' and is_hexiao=0 ORDER BY createtime DESC " . "LIMIT " . ($pindex - 1) * $psize . ',' . $psize;
	$list = pdo_fetchall($sql, $paras);
	$paytype = array('0' => array('css' => 'default', 'name' => '未支付'), '2' => array('css' => 'success', 'name' => '微信支付'));
	$orderstatus = array('0' => array('css' => 'default', 'name' => '待付款'), '1' => array('css' => 'info', 'name' => '已付款'), '2' => array('css' => 'warning', 'name' => '待发货'), '3' => array('css' => 'success', 'name' => '已发货'), '4' => array('css' => 'success', 'name' => '已签收'), '5' => array('css' => 'default', 'name' => '已取消'), '6' => array('css' => 'danger', 'name' => '待退款'), '7' => array('css' => 'default', 'name' => '已退款'),'8' => array('css' => 'info', 'name' => '待抽奖'),'9' => array('css' => 'default', 'name' => '未中奖'),'10' => array('css' => 'success', 'name' => '已中奖'));
	foreach ($list as $key => $value) {
		$s = $value['status'];
                if($value['status'] == 2 && $value['tuan_id'] !=0 && $value['islottery'] == 2){
                  $list[$key]['statuscss'] = $orderstatus[9]['css'];
                  $list[$key]['status'] = $orderstatus[9]['name'];
                }elseif($value['status'] == 2 && $value['tuan_id'] !=0 && $value['islottery'] == 1){
                  $list[$key]['statuscss'] = $orderstatus[10]['css'];
                  $list[$key]['status'] = $orderstatus[10]['name'];
                }elseif($value['status'] == 2 && $value['tuan_id'] !=0 && $value['islottery'] == 0){
                  $list[$key]['statuscss'] = $orderstatus[8]['css'];
                  $list[$key]['status'] = $orderstatus[8]['name'];  
                }else{
		  $list[$key]['statuscss'] = $orderstatus[$value['status']]['css'];
                  $list[$key]['status'] = $orderstatus[$value['status']]['name'];
                }
		$list[$key]['css'] = $paytype[$value['pay_type']]['css'];
		if ($value['pay_type'] == 2) {
			if (empty($value['transid'])) {
				$list[$key]['paytype'] = '微信支付';
			} else {
				$list[$key]['paytype'] = '微信支付';
			}
		} else {
			$list[$key]['paytype'] = $paytype[$value['pay_type']]['name'];
		}
		$goodsss = pdo_fetch("select gname,gimg from" . tablename('lottery_goods') . "where id = '{$value['g_id']}'");
		$list[$key]['gname'] = $goodsss['gname'];
		$list[$key]['gimg'] = $goodsss['gimg'];
	}
	$all = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('lottery_order') . " WHERE uniacid='{$_W['uniacid']}' and mobile<>'虚拟' and is_hexiao=0 ");
	$status0 = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('lottery_order') . " WHERE uniacid='{$_W['uniacid']}' and status=0 and mobile<>'虚拟' and is_hexiao=0");
	$status1 = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('lottery_order') . " WHERE uniacid='{$_W['uniacid']}' and status=1 and mobile<>'虚拟' and is_hexiao=0");
	$status2 = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('lottery_order') . " WHERE uniacid='{$_W['uniacid']}' and status=2 and mobile<>'虚拟' and is_hexiao=0 and tuan_id=0 and islottery=0");
	$status3 = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('lottery_order') . " WHERE uniacid='{$_W['uniacid']}' and status=3 and mobile<>'虚拟' and is_hexiao=0");
	$status4 = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('lottery_order') . " WHERE uniacid='{$_W['uniacid']}' and status=4 and mobile<>'虚拟' and is_hexiao=0");
	$status5 = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('lottery_order') . " WHERE uniacid='{$_W['uniacid']}' and status=5 and mobile<>'虚拟' and is_hexiao=0");
	$status6 = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('lottery_order') . " WHERE uniacid='{$_W['uniacid']}' and status=6 and mobile<>'虚拟' and is_hexiao=0");
	$status7 = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('lottery_order') . " WHERE uniacid='{$_W['uniacid']}' and status=7 and mobile<>'虚拟' and is_hexiao=0");
        $status8 = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('lottery_order') . " WHERE uniacid='{$_W['uniacid']}' and status=2 and mobile<>'虚拟' and is_hexiao=0 and tuan_id<>0");
        $status9 = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('lottery_order') . " WHERE uniacid='{$_W['uniacid']}' and status=2 and mobile<>'虚拟' and is_hexiao=0 and tuan_id<>0 and islottery=1");
	$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('lottery_order') . " WHERE $condition and mobile<>'虚拟' and is_hexiao=0", $paras);
	$pager = pagination($total, $pindex, $psize);
        //导出模板
        if($_GPC['submit_modal'] == "导出模板"){
        ca('order.op.export');
        $columns =array(array('title' => '订单编号', 'field' => 'ordersn', 'width' => 24),array('title' => '快递公司', 'field' => 'expresscom', 'width' => 24),array('title' => '快递单号', 'field' => 'expresssn', 'width' => 24));
        m('excel')->export(" ", array("title" => "限时抢购待发货订单模板-" . date('Y-m-d-H-i', time()), "columns" => $columns));
        }
        //批量发货
        if($_GPC['submit_expro'] == "批量发货"){
        ca('order.op.send');
        $info=m('excel')->import("import_expro");
        foreach($info as $key=>$value){
        $value[0]=trim($value[0]);$value[1]=trim($value[1]);$value[2]=trim($value[2]);
       	$order = pdo_fetch('select * from ' . tablename('lottery_order') . ' where orderno=:orderno and uniacid=:uniacid limit 1', array(':orderno' => $value[0], ':uniacid' => $_W['uniacid']));
        $goods = pdo_fetch("select * from" . tablename('lottery_goods') . "where id = '{$order['g_id']}' and uniacid='{$_W['uniacid']}'");
            if(!empty($value[0]) && !empty($value[1]) && !empty($value[2])){
            pdo_update('lottery_order',array('status'=>3,"express"=>$value[1],"expresssn"=>$value[2],"sendtime"=>time()), array("orderno"=>$value[0], 'uniacid' => $_W['uniacid'])); 
            $msg = array(
              'first' =>array('value' => "拼团抽奖订单发货通知！", "color" => "#4a5077"),
              'orderProductName' => array('title' => '商品名称', 'value' => $goods['gname'], "color" => "#4a5077"),
              'orderProductPrice' => array('title' => '商品金额', 'value' => '￥' .$order['price']. '元', "color" => "#4a5077"),
              'orderProductfuwu' => array('title' => '物流服务', 'value' => $order['express'] , "color" => "#4a5077"), 
              'orderProductdanhao' => array('title' => '快递单号', 'value' => $order['expresssn'], "color" => "#4a5077"),
              'orderProductxinxi' => array('title' => '收货信息', 'value' =>'地址：'.$order['address'].' 收货人：'.$order['addname'].' 电话:'.$order['mobile'], "color" => "#4a5077"), 
              'remark' => array('value' => "\r\n我们正在加速送到您的手上，请您耐心等待。", "color" => "#4a5077")
            );
            m('message')->sendCustomNotice($order['openid'],$msg);
           }
        }
        message('批量兑换成功！',$this->createWebUrl('allorder', array('op' => 'display','p'=>'tuangou_list','status'=>$_GPC['status'])), 'success');
        }
        //导出订单
        if ($_GPC['export'] == 1) {
            ca('order.op.export');
            $columns = array(
                array('title' => '订单编号', 'field' => 'orderno', 'width' => 24),
                array('title' => "微信订单号", 'field' => 'transid', 'width' =>24),
                array('title' => '姓名', 'field' => 'addname', 'width' => 12),
                array('title' => '手机号码', 'field' => 'mobile', 'width' => 12),
                array('title' => '总价（元）', 'field' => 'price', 'width' => 12),
                array('title' => '状态', 'field' => 'status', 'width' => 12),
                array('title' => '下单时间', 'field' => 'createtime', 'width' => 12),
                array('title' => '商品名称', 'field' => 'gname', 'width' => 12),
                array('title' => '收货地址', 'field' => 'address', 'width' => 12),
                array('title' => "微信订单号", 'field' => 'transid', 'width' =>24),
                array('title' => "地址类型", 'field' => 'addresstype', 'width' => 12)
            );
            $list= pdo_fetchall("select  * from " . tablename('lottery_order') . " where $condition and mobile<>'虚拟' and is_hexiao=0 ORDER BY createtime DESC ", $paras);
            foreach($list as $k=>$v){
                if ($v['status'] == '0') {$thisstatus = '未支付';}
		if ($v['status'] == '1') {$thisstatus = '已支付';}
		if ($v['status'] == '2' && $v['tuan_id'] == 0) {$thisstatus = '待发货';}
                if ($v['status'] == '2' && $v['tuan_id'] != 0 && $v['islottery'] ==0) {$thisstatus = '待抽奖';}
                if ($v['status'] == '2' && $v['tuan_id'] != 0 && $v['islottery'] ==1) {$thisstatus = '中奖';}
                if ($v['status'] == '2' && $v['tuan_id'] != 0 && $v['islottery'] ==2) {$thisstatus = '未中奖';}
		if ($v['status'] == '3') {$thisstatus = '已发货';}
		if ($v['status'] == '4') {$thisstatus = '已签收';}
		if ($v['status'] == '5') {$thisstatus = '已取消';}
		if ($v['status'] == '6') {$thisstatus = '待退款';}
		if ($v['status'] == '7') {$thisstatus = '已退款';}
		if ($v['status'] == '') {$thisstatus = '全部订单';}
                $list[$K]['status']=$thisstatus;
                $list[$k]['createtime'] = date('Y-m-d H:i:s', $v['createtime']);
                $goods = pdo_fetch("select * from" . tablename('lottery_goods') . "where id = '{$v['g_id']}' and uniacid='{$_W['uniacid']}'");
                $list[$k]['gname']=$goods['gname'];
                $list[$k]['addresstype']=$v['addresstype']==1?'公司':'家庭';
                $list[$k]['orderno']=" ".$v['orderno'];
                $list[$k]['transid']=" ".$v['transid'];
                $list[$k]['status']=$thisstatus;
            }
            m('excel')->export($list, array("title" => "拼团抽奖订单导出" . date('Y-m-d-H-i', time()), "columns" => $columns));
        }
        load()->func('tpl');
        include $this->template('web/allorder/lottery_list');
 } elseif ($operation == 'detail') {
        ca('order.view');
	$id = intval($_GPC['id']);
	$all = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('lottery_order') . " WHERE uniacid='{$_W['uniacid']}'  and mobile<>'虚拟' and is_hexiao=0");
        $status0 = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('lottery_order') . " WHERE uniacid='{$_W['uniacid']}' and status=0 and mobile<>'虚拟' and is_hexiao=0");
	$status1 = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('lottery_order') . " WHERE uniacid='{$_W['uniacid']}' and status=1 and mobile<>'虚拟' and is_hexiao=0");
	$status2 = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('lottery_order') . " WHERE uniacid='{$_W['uniacid']}' and status=2 and mobile<>'虚拟' and is_hexiao=0 and tuan_id=0 and islottery=0");
	$status3 = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('lottery_order') . " WHERE uniacid='{$_W['uniacid']}' and status=3 and mobile<>'虚拟' and is_hexiao=0");
	$status4 = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('lottery_order') . " WHERE uniacid='{$_W['uniacid']}' and status=4 and mobile<>'虚拟' and is_hexiao=0");
	$status5 = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('lottery_order') . " WHERE uniacid='{$_W['uniacid']}' and status=5 and mobile<>'虚拟' and is_hexiao=0");
	$status6 = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('lottery_order') . " WHERE uniacid='{$_W['uniacid']}' and status=6 and mobile<>'虚拟' and is_hexiao=0");
	$status7 = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('lottery_order') . " WHERE uniacid='{$_W['uniacid']}' and status=7 and mobile<>'虚拟' and is_hexiao=0");
        $status8 = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('lottery_order') . " WHERE uniacid='{$_W['uniacid']}' and status=2 and mobile<>'虚拟' and is_hexiao=0 and tuan_id<>0");
        $status9 = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('lottery_order') . " WHERE uniacid='{$_W['uniacid']}' and status=2 and mobile<>'虚拟' and is_hexiao=0 and tuan_id<>0 and islottery=1");
	$item = pdo_fetch("SELECT * FROM " . tablename('lottery_order') . " WHERE id = :id", array(':id' => $id));
        $userinfo = pdo_fetch("SELECT * FROM " . tablename('ewei_shop_member') . " WHERE openid = :openid", array(':openid' =>$item['openid']));
	if ($item['status'] == 7) {
		$refund_record = pdo_fetch("SELECT * FROM " . tablename('lottery_refund_record') . " WHERE orderid = :id", array(':id' => $id));
	}
	if (empty($item)) {
		message("抱歉，订单不存在!", referer(), "error");
	}

	if (checksubmit('editstatus')) {
		if ($_GPC['thestatus']=='') {
			message('请选择订单状态！');
		} else {
			pdo_update('lottery_order', array('status'=>$_GPC['thestatus']), array('id' => $id));
		}
		message('修改成功！', referer(), 'success');
	}
	if (checksubmit('getgoods')) {
		if ($_GPC['recvname']=='' || $_GPC['recvmobile']=='' || $_GPC['recvaddress']=='' || $_GPC['addresstype']=='') {
			message('请输入完整信息！');
		} else {
			pdo_update('lottery_order', array('addname' => $_GPC['recvname'], 'mobile' => $_GPC['recvmobile'], 'address' => $_GPC['recvaddress'],'addresstype'=>$_GPC['addresstype']), array('id' => $id));
		}
		message('修改成功！', referer(), 'success');
	}
	if (checksubmit('confirmsend')) {
                ca('order.op.send');
		if (!empty($_GPC['isexpress']) && empty($_GPC['expresssn'])) {
			message('请输入快递单号！');
		}
		pdo_update('lottery_order', array('status' => 3, 'express' => $_GPC['express'], 'expresssn' => $_GPC['expresssn'], 'sendtime' => TIMESTAMP), array('id' => $id));
                $order = pdo_fetch("select * from" . tablename('lottery_order') . "where id ='{$id}'");
                $goods = pdo_fetch("select * from" . tablename('lottery_goods') . "where id = '{$order['g_id']}' and uniacid='{$_W['uniacid']}'");
                $msg = array(
                  'first' =>array('value' => "拼团抽奖订单发货通知！", "color" => "#4a5077"),
                  'orderProductName' => array('title' => '商品名称', 'value' => $goods['gname'], "color" => "#4a5077"),
                  'orderProductPrice' => array('title' => '商品金额', 'value' => '￥' .$order['price']. '元', "color" => "#4a5077"),
                  'orderProductfuwu' => array('title' => '物流服务', 'value' => $order['express'] , "color" => "#4a5077"), 
                  'orderProductdanhao' => array('title' => '快递单号', 'value' => $order['expresssn'], "color" => "#4a5077"),
                  'orderProductxinxi' => array('title' => '收货信息', 'value' =>'地址：'.$order['address'].' 收货人：'.$order['addname'].' 电话:'.$order['mobile'], "color" => "#4a5077"), 
                  'remark' => array('value' => "\r\n我们正在加速送到您的手上，请您耐心等待。", "color" => "#4a5077")
                );
                m('message')->sendCustomNotice($order['openid'],$msg);
	}
	if (checksubmit('cancelsend')) {
                ca('order.op.sendcancel');
		pdo_update('lottery_order', array('status' => 2), array('id' => $id));
		message('取消发货操作成功！', referer(), 'success');
	}
	if (checksubmit('refund')) {
		ca('order.op.refund');
		$refund_id = $_GPC['refund_id'];
		//页面获取的退款订单号
		$refund_ids = pdo_fetch("select * from" . tablename('lottery_order') . "where id='{$refund_id}'");
		$res=$site->refund($refund_ids['orderno'],'',2);
		if($res=='success'){
			message('退款成功了！', referer(), 'success');
		} else {
			message('退款失败，服务器正忙，请稍等等！', referer(), 'fail');
		}
	}
	if (checksubmit('confrimpay')) {
                ca('order.op.pay');
		pdo_update('lottery_order', array('status' => 1, 'pay_type' => 2, 'remark' => $_GPC['remark']), array('id' => $id));
		// //设置库存
		// $this->setOrderStock($id);
		message('确认订单付款操作成功！', referer(), 'success');
	}
	$item['user'] = pdo_fetch("SELECT * FROM " . tablename('lottery_address') . " WHERE id = {$item['addressid']}");
	$goods = pdo_fetchall("select * from" . tablename('lottery_goods') . "WHERE id={$item['g_id']}");
	$item['goods'] = $goods;
        $item['userinfo']=$userinfo;
        load()->func('tpl');
        include $this->template('web/allorder/lottery_detail');
        exit;
} elseif ($operation == 'delete') {
        ca('order.op.delete');
	/*订单删除*/
	$orderid = intval($_GPC['id']);
	if (pdo_delete('lottery_order', array('id' => $orderid))) {
		message('订单删除成功', $this -> createWebUrl('allorder', array('op' => 'display','p'=>'lottery_list')), 'success');
	} else {
		message('订单不存在或已被删除', $this -> createWebUrl('allorder', array('op' => 'display','p'=>'lottery_list')), 'error');
	}

}elseif($operation== 'draw'){
        ca('order.op.send');
        require_once IA_ROOT . '/addons/feng_lottery/source/Message.class.php';
	$orderid = intval($_GPC['id']);
        $orderinfo=pdo_fetch("SELECT * FROM " . tablename('lottery_order') . " WHERE id = {$orderid}");
	if ($orderinfo) {
                //发送抽奖结果推送消息
                if($_GPC['islottery']==1){
                $msg = array(
                    'first' => array('value' => "拼团抽奖活动提醒！", "color" => "#4a5077"), 
                    'orderno' => array('title' => '订单号', 'value' => $orderinfo['orderno'], "color" => "#4a5077"),
                    'remark' => array(
                        'value' => "\r\n恭喜您中奖啦，详情进入拼团抽奖-我的订单中查看！\r\n\r\n小妖家会在活动结束后，24小时内安排发货，敬请留意~", 
                        "color" => "#4a5077"
                        )
                 );
                $this->sendCustomNotice($orderinfo['openid'], $msg);    
                }
                if($_GPC['islottery']==2){
                $msg = array(
                    'first' => array('value' => "拼团抽奖活动提醒！", "color" => "#4a5077"),
                    'orderno' => array('title' => '订单号', 'value' => $orderinfo['orderno'], "color" => "#4a5077"),
                    'remark' => array(
                        'value' => "\r\n姿势不对~很遗憾您未中奖，详情进入拼团抽奖-我的订单中查看！\r\n\r\n更多优惠活动尽在小妖家公众平台！",
                        "color" => "#4a5077"
                    )
                );
                $this->sendCustomNotice($orderinfo['openid'], $msg);    
                }
                pdo_update('lottery_order',array('islottery'=>$_GPC['islottery']),array('id' => $orderid));
		message('抽奖成功', $this -> createWebUrl('allorder', array('op' => 'display','status'=>8,'p'=>'lottery_list')), 'success');
	} else {
		message('订单不存在或已被删除', $this -> createWebUrl('allorder', array('op' => 'display','p'=>'lottery_list')), 'error');
	}        
}elseif($operation == 'tuikuan') {
                ca('order.op.refund');
		/*处理退款*/
				$checkboxs = $_GPC['checkbox'];
				
				$success_num =0;
				$fail_num =0;
				foreach($checkboxs as $k=>$value){
					$refund_ids = pdo_fetch("select * from".tablename('lottery_order')."where id='{$value}'");
					
					$res = $site->refund($refund_ids['orderno'],'',2);
					
					if($res == 'success'){
						$success_num+=1;
					}else{
						$fail_num+=1;
					}
				}
				message('退款操作成功，成功！'.$success_num.'人,失败'.$fail_num.'人', referer(), 'success');
		/*处理退款*/
}elseif($operation == 'choujiang') {
         ca('order.op.send');
         require_once IA_ROOT . '/addons/feng_lottery/source/Message.class.php';
		/*批量处理未中奖*/
				$checkboxs = $_GPC['checkbox'];
				
				$success_num =0;
				$fail_num =0;
				foreach($checkboxs as $k=>$value){
                                       $orderinfo=pdo_fetch("SELECT * FROM " . tablename('lottery_order') . " WHERE id = {$value}");
                                        if ($orderinfo){
                                                //发送抽奖结果推送消息
                                                $msg = array(
                                                    'first' => array('value' => "拼团抽奖活动提醒！", "color" => "#4a5077"),
                                                    'orderno' => array('title' => '订单号', 'value' => $orderinfo['orderno'], "color" => "#4a5077"),
                                                    'remark' => array(
                                                        'value' => "\r\n姿势不对~很遗憾您未中奖，详情进入拼团抽奖-我的订单中查看！\r\n\r\n更多优惠活动尽在小妖家公众平台！",
                                                        "color" => "#4a5077"
                                                    )
                                                );
                                                $this->sendCustomNotice($orderinfo['openid'], $msg);    
                                                pdo_update('lottery_order',array('islottery'=>2),array('id' =>$value));
                                                $success_num+=1;
                                        }else{
                                                $fail_num+=1;
                                        }
				}
				message('批量处理未中奖操作成功，成功！'.$success_num.'人,失败'.$fail_num.'人', referer(), 'success');
		/*批量处理未中奖*/
} elseif ($operation == 'refundall'){
        ca('order.op.refund');
	include_once '../addons/feng_lottery/WxPay.Api.php';
	$WxPayApi = new WxPayApi();

	load() -> func('communication');
	load() -> model('account');
	$accounts = uni_accounts();
	$allorders = pdo_fetchall("select * from" . tablename('lottery_order') . "where uniacid={$_W['uniacid']} and status = 1");
	$now = time();
	$num = 0;
	foreach ($allorders as $ke => $value) {
		$endtime = $value['endtime'];
		if ($now - $value['starttime'] > $endtime * 3600 && $value['transid'] != '' && $value['success'] == 1) {
			$num++;
			$fee = $value['price'] * 100;
			$refundid = $value['transid'];
			$acid = $_W['uniacid'];
			$path_cert = IA_ROOT . '/addons/feng_lottery/cert/' . $_W['uniacid'] . '/apiclient_cert.pem';
			//证书路径
			$path_key = IA_ROOT . '/addons/feng_lottery/cert/' . $_W['uniacid'] . '/apiclient_key.pem';
			//证书路径
			$key = $this -> module['config']['apikey'];
			//商户支付秘钥（API秘钥）
			$appid = $accounts[$acid]['key'];
			//身份标识（appid）
			$mchid = $this -> module['config']['mchid'];
			//微信支付商户号(mchid)
			/*$input：退款必须要的参数*/
			$input = new WxPayRefund();
			$input -> SetAppid($appid);
			$input -> SetMch_id($mchid);
			$input -> SetOp_user_id($mchid);
			$input -> SetOut_refund_no($mchid . date("YmdHis"));
			$input -> SetRefund_fee($fee);
			$input -> SetTotal_fee($fee);
			$input -> SetTransaction_id($refundid);
			$result = $WxPayApi -> refund($input, 6, $path_cert, $path_key, $key);
			if ($result['return_code'] == 'SUCCESS') {
				pdo_update('lottery_order', array('status' => 4), array('id' => $value['id']));
				pdo_query("update" . tablename('lottery_goods') . " set gnum=gnum+1 where id = '{$value['g_id']}'");
			}

		}
	}
	if ($num == 0) {
		message('未找到已付款且团购过期的微信订单。', referer(), 'fail');
	} else {
		message('一键退款成功！共处理了' . $num . '个订单。', referer(), 'success');
	}
}